package asionreachel.cn.smarthomedemo.device.cdm1;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.base.ConstantDefinition;
import asionreachel.cn.smarthomedemo.data.CAllDeviceInfoList;
import asionreachel.cn.smarthomedemo.data.CDeviceItem;
import asionreachel.cn.smarthomedemo.device.SlidingMenuMgr;
import familyclient.android.device.cdm1.JDevice_CDM1Interface;
import familyclient.android.device.cdm1.JDevice_CDM1Listener;
import familyclient.android.netswitcher.JNetSwitcherInterface;
import familyclient.android.netswitcher.JNetSwitcherListener;
import familyclient.java.device.cdm1.data.JCfgInfo1Data;
import familyclient.java.device.cdm1.data.JCfgInfo2Data;
import familyclient.java.device.cdm1.data.JCfgInfo3Data;
import familyclient.java.device.cdm1.data.JDeviceInfoData;
import familyclient.java.device.cdm1.data.JDeviceInfoListData;
import familyclient.java.device.cdm1.data.JGameResultData;
import familyclient.java.device.cdm1.data.JGameStartParamData;
import familyclient.java.device.cdm1.data.JShareInfoListData;
import familyclient.java.device.cdm1.data.JStatisticalInfoData;

public class Device_CDM1Activity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "Device_CDM1Activity";
    private static final String FUNCTION = "Device_CDM1";
    private static final String mDeviceTag = ConstantDefinition.CDM1;

    private String m_strDeviceID = "";
    private JDeviceInfoData m_deviceData = null;
    private JCfgInfo1Data m_dataCfgInfo1 = null;
    private JCfgInfo2Data m_dataCfgInfo2 = null;
    private JCfgInfo3Data m_dataCfgInfo3 = null;
    private JStatisticalInfoData m_dataStatisticalInfo = null;
    private TextView m_tvDeviceName;
    private TextView m_tvDeviceID;
    private ImageView m_ivOnline;


    private JNetSwitcherListener m_listenerNetSwitcher = new JNetSwitcherListener() {
        @Override
        public void CbInternetServerDisconnectCb() {
            if( !m_bRun )
                return;

            if( !Common.s_bDirectConnect )
                Common.showToast( "网络已断开" );
        }

        @Override
        public void CheckInternetServerConnectedCb( boolean a_bConnected ) {
            if( !m_bRun )
                return;

            if( !a_bConnected && !Common.s_bDirectConnect )
                Common.showToast( "网络已断开" );
        }

        @Override
        public void CbDeviceDisconnectCb( String a_strDeviceID ) {
            if( !m_bRun )
                return;

            if( Common.s_bDirectConnect && m_strDeviceID.equals( a_strDeviceID ) ) {
                Common.showToast("直连网络已断开");
                finish();
            }
        }

        @Override
        public void CheckDeviceConnectedCb( String a_strDeviceID, boolean a_bConnected ) {
            if( !m_bRun )
                return;

            if( Common.s_bDirectConnect && m_strDeviceID.equals( a_strDeviceID ) && !a_bConnected ) {
                Common.showToast("直连网络已断开");
                finish();
            }
        }
    };

    private JDevice_CDM1Listener m_listenerDevice_CDM1 = new JDevice_CDM1Listener()
    {
        @Override
        public void SetCfgInfo1Cb(String a_strErr) {
            super.SetCfgInfo1Cb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void SetCfgInfo2Cb(String a_strErr) {
            super.SetCfgInfo2Cb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void SetCfgInfo3Cb(String a_strErr) {
            super.SetCfgInfo3Cb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void ResetDeviceCb(String a_strErr) {
            super.ResetDeviceCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void ConfigDeviceCb(String a_strErr) {
            super.ConfigDeviceCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void SetApConfigCb(String a_strErr) {
            super.SetApConfigCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void SetFamilyCb(String a_strErr) {
            super.SetFamilyCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }



        @Override
        public void CbDeviceInfoUpdatedCb( String a_strDeviceID )
        {
            if( SlidingMenuMgr.getInstance().getDeviceTag().equals( ConstantDefinition.CDM1 ) && SlidingMenuMgr.getInstance().IsRun() )
                JDevice_CDM1Interface.GetInstance().GetDeviceInfo( a_strDeviceID );
            else if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                JDevice_CDM1Interface.GetInstance().GetDeviceInfo( a_strDeviceID );
        }

        @Override
        public void CbDeviceInfoListUpdatedCb() {
            if( SlidingMenuMgr.getInstance().getDeviceTag().equals( ConstantDefinition.CDM1 ) && SlidingMenuMgr.getInstance().IsRun() )
                JDevice_CDM1Interface.GetInstance().GetDeviceInfoList();
            else if( m_bRun )
                JDevice_CDM1Interface.GetInstance().GetDeviceInfo( m_strDeviceID );
        }

        @Override
        public void CbCfgInfo1UpdatedCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                JDevice_CDM1Interface.GetInstance().GetCfgInfo1( a_strDeviceID );
        }

        @Override
        public void CbCfgInfo2UpdatedCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                JDevice_CDM1Interface.GetInstance().GetCfgInfo2( a_strDeviceID );
        }

        @Override
        public void CbCfgInfo3UpdatedCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                JDevice_CDM1Interface.GetInstance().GetCfgInfo3( a_strDeviceID );
        }

        @Override
        public void CbStatisticalInfoUpdatedCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                JDevice_CDM1Interface.GetInstance().GetStatisticalInfo( a_strDeviceID );
        }

        @Override
        public void CbGameStartCb(String a_strDeviceID, JGameStartParamData a_dataGameStartParam) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                Common.showToast("游戏开始："+a_dataGameStartParam.m_bLaser+","+a_dataGameStartParam.m_iGripMode);
        }

        @Override
        public void CbGameBusyCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                Common.showToast("游戏繁忙");
        }

        @Override
        public void CbGameErrorCb(String a_strDeviceID) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                Common.showToast("游戏出错");
        }

        @Override
        public void GameCtrlCb(String a_strErr) {
            super.GameCtrlCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }



        @Override
        public void CbGameFinishCb(String a_strDeviceID, JGameResultData a_dataGameResult) {
            if( m_bRun && m_strDeviceID.equals( a_strDeviceID ) )
                Common.showToast("游戏结束："+a_dataGameResult.m_bWin);
        }


        @Override
        public void GetDeviceInfoCb(JDeviceInfoData a_dataDeviceInfo, String a_strErr )
        {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    if( m_strDeviceID.equals( a_dataDeviceInfo.m_strDeviceID ) )
                    {
                        m_deviceData = a_dataDeviceInfo;
                        UpdateView();
                    }
                }
                if( SlidingMenuMgr.getInstance().getDeviceTag().equals( ConstantDefinition.CDM1 ) && SlidingMenuMgr.getInstance().IsRun() )
                {
                    CAllDeviceInfoList.GetInstance().notifyDeviceInfoChange( new CDeviceItem( a_dataDeviceInfo.m_strDeviceID, a_dataDeviceInfo.m_strDeviceName, a_dataDeviceInfo.m_bOnline, R.drawable.cdm1_online, R.drawable.cdm1_offline ) );
                }
            }
        }

        @Override
        public void SetShareInfoCb(String a_strErr) {
            super.SetShareInfoCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void GetShareInfoListCb(JShareInfoListData a_listShareInfo, String a_strErr) {
            super.GetShareInfoListCb(a_listShareInfo, a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void ClearAllShareInfoCb(String a_strErr) {
            super.ClearAllShareInfoCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void SetDeviceNameCb(String a_strErr) {
            super.SetDeviceNameCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }


        @Override
        public void GetDeviceInfoListCb(JDeviceInfoListData a_listDeviceInfo, String a_strErr)
        {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    m_deviceData = a_listDeviceInfo.GetData( m_strDeviceID );
                    UpdateView();
                }
                if( SlidingMenuMgr.getInstance().getDeviceTag().equals( ConstantDefinition.CDM1 ) && SlidingMenuMgr.getInstance().IsRun() )
                {
                    ArrayList<CDeviceItem> listDeviceItem = new ArrayList<>();
                    for( int iIndex = 0 ; iIndex < a_listDeviceInfo.GetSize() ; ++iIndex ) {
                        JDeviceInfoData dataDeviceInfo = a_listDeviceInfo.GetData( iIndex );
                        listDeviceItem.add(new CDeviceItem(dataDeviceInfo.m_strDeviceID, dataDeviceInfo.m_strDeviceName, dataDeviceInfo.m_bOnline, R.drawable.cdm1_online, R.drawable.cdm1_offline ));
                    }
                    CAllDeviceInfoList.GetInstance().notifyDeviceInfoChange( listDeviceItem );
                }
            }
        }

        @Override
        public void GetCfgInfo1Cb(JCfgInfo1Data a_dataCfgInfo1, String a_strErr) {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    m_dataCfgInfo1 = a_dataCfgInfo1;
                    UpdateView();
                }
            }
        }

        @Override
        public void GetCfgInfo2Cb(JCfgInfo2Data a_dataCfgInfo2, String a_strErr) {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    m_dataCfgInfo2 = a_dataCfgInfo2;
                    UpdateView();
                }
            }
        }

        @Override
        public void GetCfgInfo3Cb(JCfgInfo3Data a_dataCfgInfo3, String a_strErr) {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    m_dataCfgInfo3 = a_dataCfgInfo3;
                    UpdateView();
                }
            }
        }

        @Override
        public void ClearStatisticalInfoCb(String a_strErr) {
            super.ClearStatisticalInfoCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void ApplyGameCb(String a_strErr) {
            super.ApplyGameCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void AbortGameCb(String a_strErr) {
            super.AbortGameCb(a_strErr);
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
        }

        @Override
        public void GetStatisticalInfoCb(JStatisticalInfoData a_dataStatisticalInfo, String a_strErr) {
            if( null != a_strErr && a_strErr.length() > 0 )
            {
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                if( m_bRun )
                {
                    m_dataStatisticalInfo = a_dataStatisticalInfo;
                    UpdateView();
                }
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        m_strDeviceID = getIntent().getStringExtra( "DeviceID" );

        m_deviceData = null;

        JNetSwitcherInterface.GetInstance().AddListener( m_listenerNetSwitcher );
        JDevice_CDM1Interface.GetInstance().AddListener( m_listenerDevice_CDM1 );

        setContentView(R.layout.device_ctrl_cdm1_activity);
        mActivity = this;
        m_tvDeviceName = (TextView)findViewById(R.id.device_name);
        m_tvDeviceID = (TextView)findViewById(R.id.device_id);
        m_ivOnline = (ImageView)findViewById(R.id.device_online);
        ImageView btBack = (ImageView)findViewById(R.id.iv_return);
        Button btRefresh = (Button)findViewById(R.id.refresh_bt);
        Button btClearStatistical = (Button)findViewById(R.id.clearstatistical_bt);
        Button btSetCfg = (Button)findViewById(R.id.setcfg_bt);
        Button btApplyGame = (Button)findViewById(R.id.applygame_bt);
        Button btAbortGame = (Button)findViewById(R.id.abortgame_bt);
        Button btX1 = (Button)findViewById(R.id.x1_bt);
        Button btX2 = (Button)findViewById(R.id.x2_bt);
        Button btY1 = (Button)findViewById(R.id.y1_bt);
        Button btY2 = (Button)findViewById(R.id.y2_bt);
        Button btCatch = (Button)findViewById(R.id.catch_bt);

        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });

        btRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( !Common.s_bDirectConnect ) {
                    JDevice_CDM1Interface.GetInstance().GetDeviceInfo(m_strDeviceID);
                    JDevice_CDM1Interface.GetInstance().GetCfgInfo1(m_strDeviceID);
                    JDevice_CDM1Interface.GetInstance().GetCfgInfo2(m_strDeviceID);
                    JDevice_CDM1Interface.GetInstance().GetCfgInfo3(m_strDeviceID);
                    JDevice_CDM1Interface.GetInstance().GetStatisticalInfo(m_strDeviceID);
                }
            }
        });

        btClearStatistical.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( !Common.s_bDirectConnect ) {
                    JDevice_CDM1Interface.GetInstance().ClearStatisticalInfo(m_strDeviceID);
                }
            }
        });

        btSetCfg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText m_etGamePrice = (EditText)findViewById(R.id.gameprice_et);
                int iGamePrice = Integer.valueOf( m_etGamePrice.getText().toString() );

                EditText m_etGameTime = (EditText)findViewById(R.id.gametime_et);
                int iGameTime = Integer.valueOf( m_etGameTime.getText().toString() );

                EditText m_etMusic = (EditText)findViewById(R.id.music_et);
                int iMusic = Integer.valueOf( m_etMusic.getText().toString() );

                EditText m_etGripInAir = (EditText)findViewById(R.id.gripinair_et);
                boolean bGripInAir = Boolean.valueOf( m_etGripInAir.getText().toString() );

                EditText m_etGiftExport = (EditText)findViewById(R.id.giftexport_et);
                int iGiftExport = Integer.valueOf( m_etGiftExport.getText().toString() );

                EditText m_etCtrlMode = (EditText)findViewById(R.id.ctrlmode_et);
                int iCtrlMode = Integer.valueOf( m_etCtrlMode.getText().toString() );

                EditText m_etVolume = (EditText)findViewById(R.id.volume_et);
                int iVolume = Integer.valueOf( m_etVolume.getText().toString() );

                EditText m_etCoinSignal = (EditText)findViewById(R.id.coinsignal_et);
                boolean bCoinSignal = Boolean.valueOf( m_etCoinSignal.getText().toString() );

                EditText m_etLuckyPrize = (EditText)findViewById(R.id.luckyprize_et);
                boolean bLuckyPrize = Boolean.valueOf( m_etLuckyPrize.getText().toString() );

                EditText m_etLanguage = (EditText)findViewById(R.id.language_et);
                int iLanguage = Integer.valueOf( m_etLanguage.getText().toString() );

                EditText m_etXSpeed = (EditText)findViewById(R.id.xspeed_et);
                int iXSpeed = Integer.valueOf( m_etXSpeed.getText().toString() );

                EditText m_etYSpeed = (EditText)findViewById(R.id.yspeed_et);
                int iYSpeed = Integer.valueOf( m_etYSpeed.getText().toString() );

                EditText m_etZSpeed = (EditText)findViewById(R.id.zspeed_et);
                int iZSpeed = Integer.valueOf( m_etZSpeed.getText().toString() );

                EditText m_etZLength = (EditText)findViewById(R.id.zlength_et);
                int iZLength = Integer.valueOf( m_etZLength.getText().toString() );

                EditText m_etCatchGripVoltage = (EditText)findViewById(R.id.catchgripvoltage_et);
                int iCatchGripVoltage = Integer.valueOf( m_etCatchGripVoltage.getText().toString() );

                EditText m_etTopGripVoltage = (EditText)findViewById(R.id.topgripvoltage_et);
                int iTopGripVoltage = Integer.valueOf( m_etTopGripVoltage.getText().toString() );

                EditText m_etMoveGripVoltage = (EditText)findViewById(R.id.movegripvoltage_et);
                int iMoveGripVoltage = Integer.valueOf( m_etMoveGripVoltage.getText().toString() );

                EditText m_etStrongGripVoltage = (EditText)findViewById(R.id.stronggripvoltage_et);
                int iStrongGripVoltage = Integer.valueOf( m_etStrongGripVoltage.getText().toString() );

                EditText m_etStrongGripTime = (EditText)findViewById(R.id.stronggriptime_et);
                int iStrongGripTime = Integer.valueOf( m_etStrongGripTime.getText().toString() );

                EditText m_etCatchMode = (EditText)findViewById(R.id.catchmode_et);
                int iCatchMode = Integer.valueOf( m_etCatchMode.getText().toString() );

                EditText m_etPressValidTime = (EditText)findViewById(R.id.pressvalidtime_et);
                int iPressValidTime = Integer.valueOf( m_etPressValidTime.getText().toString() );

                EditText m_etGiftPrice = (EditText)findViewById(R.id.giftprice_et);
                int iGiftPrice = Integer.valueOf( m_etGiftPrice.getText().toString() );

                if( !Common.s_bDirectConnect ) {
                    JDevice_CDM1Interface.GetInstance().SetCfgInfo1( m_strDeviceID, iGamePrice, iGameTime, iMusic, bGripInAir, iGiftExport, iCtrlMode, iVolume, bCoinSignal );
                    JDevice_CDM1Interface.GetInstance().SetCfgInfo2( m_strDeviceID, bLuckyPrize, iLanguage, iXSpeed, iYSpeed, iZSpeed, iZLength, iCatchGripVoltage );
                    JDevice_CDM1Interface.GetInstance().SetCfgInfo3( m_strDeviceID, iTopGripVoltage, iMoveGripVoltage, iStrongGripVoltage, iStrongGripTime, iCatchMode, iPressValidTime, iGiftPrice );
                }
            }
        });

        btApplyGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText m_etLaser = (EditText)findViewById(R.id.laser_et);
                boolean bLaser = Boolean.valueOf( m_etLaser.getText().toString() );

                EditText m_etGripMode = (EditText)findViewById(R.id.gripmode_et);
                int iGripMode = Integer.valueOf( m_etGripMode.getText().toString() );

                if( !Common.s_bDirectConnect ) {
                    JDevice_CDM1Interface.GetInstance().ApplyGame( m_strDeviceID, bLaser, iGripMode );
                }
            }
        });

        btAbortGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( !Common.s_bDirectConnect ) {
                    JDevice_CDM1Interface.GetInstance().AbortGame( m_strDeviceID );
                }
            }
        });

        btX1.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch ( event.getAction() )
                {
                    case MotionEvent.ACTION_DOWN:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "10000" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00000" );
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        } );

        btX2.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch ( event.getAction() )
                {
                    case MotionEvent.ACTION_DOWN:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "01000" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00000" );
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        } );

        btY1.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch ( event.getAction() )
                {
                    case MotionEvent.ACTION_DOWN:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00100" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00000" );
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        } );

        btY2.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch ( event.getAction() )
                {
                    case MotionEvent.ACTION_DOWN:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00010" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00000" );
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        } );

        btCatch.setOnTouchListener( new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch ( event.getAction() )
                {
                    case MotionEvent.ACTION_DOWN:
                        if( !Common.s_bDirectConnect ) {
                            JDevice_CDM1Interface.GetInstance().GameCtrl( m_strDeviceID, "00001" );
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        // Only press
                        break;
                    default:
                        break;
                }
                return false;
            }
        } );

        UpdateView();

        if( !Common.s_bDirectConnect )
            SlidingMenuMgr.getInstance().initMenuLeft(mActivity);
    }

    protected void UpdateView()
    {
        if( null != m_deviceData && null != m_deviceData.m_strDeviceID && 0 != m_deviceData.m_strDeviceID.length() )
        {
            if( null == m_deviceData.m_strDeviceName || 0 == m_deviceData.m_strDeviceName.length() )
                m_tvDeviceName.setText( m_deviceData.m_strDeviceID );
            else
                m_tvDeviceName.setText( m_deviceData.m_strDeviceName );
            m_tvDeviceID.setText( m_deviceData.m_strDeviceID );
            if( m_deviceData.m_bOnline ) {
                m_ivOnline.setImageResource(R.drawable.green_icon);
            }
            else {
                m_ivOnline.setImageResource(R.drawable.gray_icon);
            }

            String strDeviceInfo = "";
            strDeviceInfo += "WorkMode:" + m_deviceData.m_iWorkMode + "; ";
            strDeviceInfo += "FaultCode:" + m_deviceData.m_iFaultCode + "; ";
            strDeviceInfo += "Win:" + m_deviceData.m_bWin;

            if( null != m_dataStatisticalInfo )
            {
                strDeviceInfo += "; ";
                strDeviceInfo += "PlayCount:" + m_dataStatisticalInfo.m_lPlayCount + "; ";
                strDeviceInfo += "GiftCount:" + m_dataStatisticalInfo.m_lGiftCount;
            }

            TextView m_tvDeviceInfo = (TextView)findViewById(R.id.device_info);
            m_tvDeviceInfo.setText( strDeviceInfo );

            EditText m_etLaser = (EditText)findViewById(R.id.laser_et);
            m_etLaser.setText( String.valueOf( m_deviceData.m_bLaser ) );

            EditText m_etGripMode = (EditText)findViewById(R.id.gripmode_et);
            m_etGripMode.setText( String.valueOf( m_deviceData.m_iGripMode ) );
        }

        if( null != m_dataCfgInfo1 )
        {
            EditText m_etGamePrice = (EditText)findViewById(R.id.gameprice_et);
            m_etGamePrice.setText( String.valueOf( m_dataCfgInfo1.m_iGamePrice ) );

            EditText m_etGameTime = (EditText)findViewById(R.id.gametime_et);
            m_etGameTime.setText( String.valueOf( m_dataCfgInfo1.m_iGameTime ) );

            EditText m_etMusic = (EditText)findViewById(R.id.music_et);
            m_etMusic.setText( String.valueOf( m_dataCfgInfo1.m_iMusic ) );

            EditText m_etGripInAir = (EditText)findViewById(R.id.gripinair_et);
            m_etGripInAir.setText( String.valueOf( m_dataCfgInfo1.m_bGripInAir ) );

            EditText m_etGiftExport = (EditText)findViewById(R.id.giftexport_et);
            m_etGiftExport.setText( String.valueOf( m_dataCfgInfo1.m_iGiftExport ) );

            EditText m_etCtrlMode = (EditText)findViewById(R.id.ctrlmode_et);
            m_etCtrlMode.setText( String.valueOf( m_dataCfgInfo1.m_iCtrlMode ) );

            EditText m_etVolume = (EditText)findViewById(R.id.volume_et);
            m_etVolume.setText( String.valueOf( m_dataCfgInfo1.m_iVolume ) );

            EditText m_etCoinSignal = (EditText)findViewById(R.id.coinsignal_et);
            m_etCoinSignal.setText( String.valueOf( m_dataCfgInfo1.m_bCoinSignal ) );
        }

        if( null != m_dataCfgInfo2 )
        {
            EditText m_etLuckyPrize = (EditText)findViewById(R.id.luckyprize_et);
            m_etLuckyPrize.setText( String.valueOf( m_dataCfgInfo2.m_bLuckyPrize ) );

            EditText m_etLanguage = (EditText)findViewById(R.id.language_et);
            m_etLanguage.setText( String.valueOf( m_dataCfgInfo2.m_iLanguage ) );

            EditText m_etXSpeed = (EditText)findViewById(R.id.xspeed_et);
            m_etXSpeed.setText( String.valueOf( m_dataCfgInfo2.m_iXSpeed ) );

            EditText m_etYSpeed = (EditText)findViewById(R.id.yspeed_et);
            m_etYSpeed.setText( String.valueOf( m_dataCfgInfo2.m_iYSpeed ) );

            EditText m_etZSpeed = (EditText)findViewById(R.id.zspeed_et);
            m_etZSpeed.setText( String.valueOf( m_dataCfgInfo2.m_iZSpeed ) );

            EditText m_etZLength = (EditText)findViewById(R.id.zlength_et);
            m_etZLength.setText( String.valueOf( m_dataCfgInfo2.m_iZLength ) );

            EditText m_etCatchGripVoltage = (EditText)findViewById(R.id.catchgripvoltage_et);
            m_etCatchGripVoltage.setText( String.valueOf( m_dataCfgInfo2.m_iCatchGripVoltage ) );
        }

        if( null != m_dataCfgInfo3 )
        {
            EditText m_etTopGripVoltage = (EditText)findViewById(R.id.topgripvoltage_et);
            m_etTopGripVoltage.setText( String.valueOf( m_dataCfgInfo3.m_iTopGripVoltage ) );

            EditText m_etMoveGripVoltage = (EditText)findViewById(R.id.movegripvoltage_et);
            m_etMoveGripVoltage.setText( String.valueOf( m_dataCfgInfo3.m_iMoveGripVoltage ) );

            EditText m_etStrongGripVoltage = (EditText)findViewById(R.id.stronggripvoltage_et);
            m_etStrongGripVoltage.setText( String.valueOf( m_dataCfgInfo3.m_iStrongGripVoltage ) );

            EditText m_etStrongGripTime = (EditText)findViewById(R.id.stronggriptime_et);
            m_etStrongGripTime.setText( String.valueOf( m_dataCfgInfo3.m_iStrongGripTime ) );

            EditText m_etCatchMode = (EditText)findViewById(R.id.catchmode_et);
            m_etCatchMode.setText( String.valueOf( m_dataCfgInfo3.m_iCatchMode ) );

            EditText m_etPressValidTime = (EditText)findViewById(R.id.pressvalidtime_et);
            m_etPressValidTime.setText( String.valueOf( m_dataCfgInfo3.m_iPressValidTime ) );

            EditText m_etGiftPrice = (EditText)findViewById(R.id.giftprice_et);
            m_etGiftPrice.setText( String.valueOf( m_dataCfgInfo3.m_iGiftPrice ) );
        }
    }

    //返回上一层
    private void onBack()
    {
        if( Common.s_bDirectConnect )
            JNetSwitcherInterface.GetInstance().DisconnectDevice( m_strDeviceID );
        finish();
    }

    @Override
    protected void onDestroy()
    {
        JDevice_CDM1Interface.GetInstance().DelListener( m_listenerDevice_CDM1 );
        JNetSwitcherInterface.GetInstance().DelListener( m_listenerNetSwitcher );
        super.onDestroy();
    }

    @Override
    public void onBackKey()
    {
        onBack();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if( !Common.s_bDirectConnect ) {
            JNetSwitcherInterface.GetInstance().CheckInternetServerConnected();
            JDevice_CDM1Interface.GetInstance().GetDeviceInfo(m_strDeviceID);
            JDevice_CDM1Interface.GetInstance().GetCfgInfo1(m_strDeviceID);
            JDevice_CDM1Interface.GetInstance().GetCfgInfo2(m_strDeviceID);
            JDevice_CDM1Interface.GetInstance().GetCfgInfo3(m_strDeviceID);
            JDevice_CDM1Interface.GetInstance().GetStatisticalInfo(m_strDeviceID);

            Common.saveDeviceIDWithTag( ConstantDefinition.CDeviceInfo.getTagByDeviceID( m_strDeviceID ), m_strDeviceID );
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            default:
                break;
        }
    }
}
